import time

from selenium import webdriver

# 打开浏览器
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://mail.163.com/')
driver.maximize_window()
# 添加需要的cookie
cookie = [
{'name': 'cm_last_info', 'value': 'dT13dTk0Mjk4MTk1NCU0MDE2My5jb20mZD1odHRwcyUzQSUyRiUyRm1haWwuMTYzLmNvbSUyRmpzNiUyRm1haW4uanNwJTNGc2lkJTNEWER1TUNFYk9MWm9Hc1pWY2tIT09Jd0hiU3pTTkRMSEgmcz1YRHVNQ0ViT0xab0dzWlZja0hPT0l3SGJTelNORExISCZoPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RYRHVNQ0ViT0xab0dzWlZja0hPT0l3SGJTelNORExISCZ3PWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJmw9LTEmdD0tMSZhcz10cnVl'},
{'name': 'Coremail', 'value': 'cb17a27e4a96a%XDuMCEbOLZoGsZVckHOOIwHbSzSNDLHH%g1a149.mail.163.com'},
{'name': 'MAIL_ENTRY_CS', 'value': '88581fcffce9f1cef9bff6728addee26'},
{'name': 'MAIL_ENTRY_INFO', 'value': '1|0|mail163|mail163_letter|183.95.62.107|d1eb9dc2122ee7092bac699c3ffb507e_v1|'},
{'name': 'NTES_SESS', 'value': '2SVFwUjUwZ22nm2XR4Q2pCT2frQa98ZFjLULOJDzZ01WTPr9Tw.qDBQPT8KLxe4XG2poVWrkTjxjMzvv4i6K15FPzt7odqnmjxs__Oo3IKXaPcXmC6s6N5D_oVb62UqUR9rQOsoNR.01RQhNt4IVAQVr6iDJDSftY_n878WaF7uC.fNaUFIF6LkEg61aedk4brroxomVmUUcB'}]
# 添加cookie

for i in cookie:
    driver.add_cookie(i)

# 刷新浏览器
driver.refresh()


# 定位到写信功能
driver.find_element(By.ID, '_mail_component_149_149').click()

# 收件人框输入邮箱
driver.find_element(By.CLASS_NAME,'nui-editableAddr-ipt').send_keys('周星驰')

# 定位到主题输入内容
time.sleep(2)
driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input').send_keys('喜剧之王')

# 切换到内容框frame
iframe = driver.find_elements(By.TAG_NAME, 'iframe')[3]
driver.switch_to.frame(iframe)

# 在内容框输入内容
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, 'body').send_keys('无厘头喜剧')

# 回到主页面
time.sleep(2)
driver.switch_to.default_content()

# 定位发送按钮并点击
time.sleep(2)
driver.find_elements(By.CLASS_NAME, 'nui-btn-icon')[2].click()

# 退出浏览器
time.sleep(2)
driver.quit()